﻿using System.Collections.Generic;
using System.Linq;
using CML.Intercamber.Business.Constants;
using CML.Intercamber.Business.Model;

namespace CML.Intercamber.Business.Dao
{
    public class EstablishmentDao
    {
        public static List<EstablishmentsDetail> ListEstablishments()
        {
            List<EstablishmentsDetail> res;
            using (var context = new IntercamberEntities())
            {
                res = (from c in context.Establishments
                       join u in context.Users on c.IdEstablishment equals u.IdEstablishment into ju
                       from lju in ju.DefaultIfEmpty()
                       group new { c, lju } by new
                       {
                           c.IdEstablishment,
                           c.LblEstablishment,
                           c.IdCountry,
                           c.TimeZone,
                           c.City, 
                           c.MapPosX, 
                           c.MapPosY
                       } into g
                       select new EstablishmentsDetail
                       {
                           IdEstablishment = g.Key.IdEstablishment,
                           LblEstablishment = g.Key.LblEstablishment,
                           IdCountry = g.Key.IdCountry,
                           TimeZone = g.Key.TimeZone,
                           City = g.Key.City,
                           MapPosX = g.Key.MapPosX, 
                           MapPosY = g.Key.MapPosY, 
                           NumberTeachers = g.Count(x => x.lju.IdProfil == ProfilsConst.Teacher.Id || x.lju.IdProfil == ProfilsConst.Admin.Id),
                           NumberStudents = g.Count(x => x.lju.IdProfil == ProfilsConst.Student.Id)
                       }).ToList();
            }
            return res;
        }

        public static void UpdateEstablishment(Establishments e)
        {
            using (var context = new IntercamberEntities())
            {
                var est = context.Establishments.FirstOrDefault(x => x.IdEstablishment == e.IdEstablishment);
                if (est != null)
                {
                    est.LblEstablishment = e.LblEstablishment;
                    est.IdCountry = e.IdCountry;
                    est.TimeZone = e.TimeZone;
                    est.City = e.City;
                    est.MapPosX = e.MapPosX;
                    est.MapPosY = e.MapPosY;
                    context.SaveChanges();
                }
            }
        }

        public static void DeleteEstablishment(int idEstablishment)
        {
            using (var context = new IntercamberEntities())
            {
                var est = context.Establishments.FirstOrDefault(x => x.IdEstablishment == idEstablishment);
                if (est != null)
                {
                    context.Establishments.Remove(est);
                    context.SaveChanges();
                }
            }

        }

        public static void InsertEstablishment(Establishments e)
        {
            using (var context = new IntercamberEntities())
            {
                context.Establishments.Add(e);
                context.SaveChanges();
           }
        }

    }
}
